package com.mall.hadoop.job.abilitystatic;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 合并八大能力分数
 * @author tgy
 *
 */
public class AbilityStaticReduce extends Reducer<Text, IntWritable, Text, IntWritable>{

	public static Logger logger = LoggerFactory.getLogger(AbilityStaticReduce.class);
	
	@Override
	protected void reduce(Text key, Iterable<IntWritable> iter,Context context)throws IOException, InterruptedException {
		try{
			int province = 0;
			int city = 0;
			int area = 0;
			//总分数
			int totalScore = 0;
			//社交能力分数
			int social = 0;
			//艺术能力分数
			int art = 0;
			//语言能力分数
			int language = 0;
			//认知能力分数
			int cognition = 0;
			//自理能力分数
			int themselves = 0;
			//运动能力分数
			int motion = 0;
			//精细能力分数
			int fine = 0;
			//数学能力分数
			int mathematics = 0;
			String k = key.toString();
			//总数计算
			int cun = 0;
			for(IntWritable wr : iter){
				cun++;
				//同一省（包括管辖的市、区）
				if(k.contains("sf_sf")||k.contains("sh_sh")||k.contains("sq_dq")){
					province += wr.get();
				}
				//同一市（包括管辖的区）
				if(k.contains("sh_sh")||k.contains("sq_dq")){
					city += wr.get();
				}
				//计算同一省
				if(k.contains("sq_dq")){
					area += wr.get();
				}
				if(k.contains("sf_1")){
					totalScore += wr.get();
				}
				if(k.contains("sf_2")){
					social += wr.get();
				}
				if(k.contains("sf_3")){
					art += wr.get();
				}
				if(k.contains("sf_4")){
					language += wr.get();
				}
				if(k.contains("sf_5")){
					cognition += wr.get();
				}
				if(k.contains("sf_6")){
					themselves += wr.get();
				}
				if(k.contains("sf_7")){
					motion += wr.get();
				}
				if(k.contains("sf_8")){
					fine += wr.get();
				}
				if(k.contains("sf_9")){
					mathematics += wr.get();
				}
				if(k.contains("sh_1")){
					totalScore += wr.get();
				}
				if(k.contains("sh_2")){
					social += wr.get();
				}
				if(k.contains("sh_3")){
					art += wr.get();
				}
				if(k.contains("sh_4")){
					language += wr.get();
				}
				if(k.contains("sh_5")){
					cognition += wr.get();
				}
				if(k.contains("sh_6")){
					themselves += wr.get();
				}
				if(k.contains("sh_7")){
					motion += wr.get();
				}
				if(k.contains("sh_8")){
					fine += wr.get();
				}
				if(k.contains("sh_9")){
					mathematics += wr.get();
				}
				if(k.contains("sq_1")){
					totalScore += wr.get();
				}
				if(k.contains("sq_2")){
					social += wr.get();
				}
				if(k.contains("sq_3")){
					art += wr.get();
				}
				if(k.contains("sq_4")){
					language += wr.get();
				}
				if(k.contains("sq_5")){
					cognition += wr.get();
				}
				if(k.contains("sq_6")){
					themselves += wr.get();
				}
				if(k.contains("sq_7")){
					motion += wr.get();
				}
				if(k.contains("sq_8")){
					fine += wr.get();
				}
				if(k.contains("sq_9")){
					mathematics += wr.get();
				}
			}
			//省份统计相关
			if(k.contains("sf_sf")){
				context.write(key, new IntWritable(province));
			}
			if(k.contains("sf_1")){
				context.write(key, new IntWritable((int)Math.rint(totalScore/cun)));
			}
			if(k.contains("sf_2")){
				context.write(key, new IntWritable((int)Math.rint(social/cun)));
			}
			if(k.contains("sf_3")){
				context.write(key, new IntWritable((int)Math.rint(art/cun)));
			}
			if(k.contains("sf_4")){
				context.write(key, new IntWritable((int)Math.rint(language/cun)));
			}
			if(k.contains("sf_5")){
				context.write(key, new IntWritable((int)Math.rint(cognition/cun)));
			}
			if(k.contains("sf_6")){
				context.write(key, new IntWritable((int)Math.rint(themselves/cun)));
			}
			if(k.contains("sf_7")){
				context.write(key, new IntWritable((int)Math.rint(motion/cun)));
			}
			if(k.contains("sf_8")){
				context.write(key, new IntWritable((int)Math.rint(fine/cun)));
			}
			if(k.contains("sf_9")){
				context.write(key, new IntWritable((int)Math.rint(mathematics/cun)));
			}
			//市统计相关
			if(k.contains("sh_sh")){
				context.write(key, new IntWritable(city));
			}
			if(k.contains("sh_1")){
				context.write(key, new IntWritable((int)Math.rint(totalScore/cun)));
			}
			if(k.contains("sh_2")){
				context.write(key, new IntWritable((int)Math.rint(social/cun)));
			}
			if(k.contains("sh_3")){
				context.write(key, new IntWritable((int)Math.rint(art/cun)));
			}
			if(k.contains("sh_4")){
				context.write(key, new IntWritable((int)Math.rint(language/cun)));
			}
			if(k.contains("sh_5")){
				context.write(key, new IntWritable((int)Math.rint(cognition/cun)));
			}
			if(k.contains("sh_6")){
				context.write(key, new IntWritable((int)Math.rint(themselves/cun)));
			}
			if(k.contains("sh_7")){
				context.write(key, new IntWritable((int)Math.rint(motion/cun)));
			}
			if(k.contains("sh_8")){
				context.write(key, new IntWritable((int)Math.rint(fine/cun)));
			}
			if(k.contains("sh_9")){
				context.write(key, new IntWritable((int)Math.rint(mathematics/cun)));
			}
			//区统计相关
			if(k.contains("sq_dq")){
				context.write(key, new IntWritable(area));
			}
			if(k.contains("sq_1")){
				context.write(key, new IntWritable((int)Math.rint(totalScore/cun)));
			}
			if(k.contains("sq_2")){
				context.write(key, new IntWritable((int)Math.rint(social/cun)));
			}
			if(k.contains("sq_3")){
				context.write(key, new IntWritable((int)Math.rint(art/cun)));
			}
			if(k.contains("sq_4")){
				context.write(key, new IntWritable((int)Math.rint(language/cun)));
			}
			if(k.contains("sq_5")){
				context.write(key, new IntWritable((int)Math.rint(cognition/cun)));
			}
			if(k.contains("sq_6")){
				context.write(key, new IntWritable((int)Math.rint(themselves/cun)));
			}
			if(k.contains("sq_7")){
				context.write(key, new IntWritable((int)Math.rint(motion/cun)));
			}
			if(k.contains("sq_8")){
				context.write(key, new IntWritable((int)Math.rint(fine/cun)));
			}
			if(k.contains("sq_9")){
				context.write(key, new IntWritable((int)Math.rint(mathematics/cun)));
			}
		}catch(Exception e){
			logger.info("合并各项能力出现异常："+e.toString());
		}
	}

}
